package com.runnor.common.websys.system.service.impl;

import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.runnor.common.websys.system.entity.SysMenu;
import com.runnor.common.websys.system.entity.bo.SysMenuBo;
import com.runnor.common.websys.system.mapper.SysMenuMapper;
import com.runnor.common.websys.system.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author runnor
 * @description 针对表【sys_menu】的数据库操作Service实现
 * @createDate 2023-08-17 10:47:26
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu>
        implements SysMenuService {

    @Autowired
    SysMenuMapper sysMenuMapper;

    @Override
    public Page pages(Page page, QueryWrapper<SysMenu> queryWrapper) {
        return sysMenuMapper.pages(page, queryWrapper);
    }

    @Override
    public List<SysMenuBo> getSysMenuListTree() {

        QueryWrapper queryWrapper = new QueryWrapper();

        queryWrapper.eq("type", "菜单");
        queryWrapper.eq("deleted", 0);
//        queryWrapper.eq("enable", 1);
        queryWrapper.orderByAsc("sort");

        List<SysMenu> list = sysMenuMapper.selectList(queryWrapper);

        List build = TreeUtil.build(list, "", TreeNodeConfig.DEFAULT_CONFIG, (sysMenuBo, tree) -> {
            tree.setName(sysMenuBo.getName());
            tree.setId(sysMenuBo.getNo());
            tree.setParentId(sysMenuBo.getParentNo());
            tree.setWeight(sysMenuBo.getSort());
        });

        return build;
    }

    @Override
    public List<SysMenuBo> getSysMenuListTreeAll() {

        QueryWrapper queryWrapper = new QueryWrapper();

        queryWrapper.eq("deleted", 0);
        queryWrapper.eq("enable", 1);
        queryWrapper.orderByAsc("sort");

        List<SysMenu> list = sysMenuMapper.selectList(queryWrapper);

        List build = TreeUtil.build(list, "", TreeNodeConfig.DEFAULT_CONFIG, (sysMenuBo, tree) -> {
            tree.setName(sysMenuBo.getName());
            tree.setId(sysMenuBo.getNo());
            tree.setParentId(sysMenuBo.getParentNo());
            tree.setWeight(sysMenuBo.getSort());
        });

        return build;
    }

    @Override
    public List<SysMenuBo> getSysMenuRoutesTree() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("type", "菜单");
        queryWrapper.eq("deleted", 0);
        queryWrapper.eq("enable", 1);
        queryWrapper.orderByAsc("sort");
        List<SysMenu> list = sysMenuMapper.selectList(queryWrapper);

        List build = TreeUtil.build(list, "", TreeNodeConfig.DEFAULT_CONFIG, (sysMenuBo, tree) -> {
            tree.setId(sysMenuBo.getNo());
            tree.setParentId(sysMenuBo.getParentNo());
            tree.setWeight(sysMenuBo.getSort());
            tree.putExtra("path", sysMenuBo.getUrl());
            tree.setName(sysMenuBo.getComponent());
            tree.putExtra("componentPath", sysMenuBo.getComponentParams());
            tree.putExtra("meta", new LinkedHashMap<String, Object>() {{
                this.put("icon", sysMenuBo.getIcon());
                this.put("title", sysMenuBo.getName());
                this.put("isCache", true);
                this.put("isNav", true);
                this.put("permission", sysMenuBo.getPermission());
            }});
        });
        return build;
    }
}




